import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

enum permissionType {
  storage, //存储
  camera, //摄像头
  microphone, //麦克风
  notification, //通知
  photo, //相册
  location, // 位置
}
Map _permissionNames = {
  permissionType.storage: "存储",
  permissionType.camera: "摄像头",
  permissionType.microphone: "麦克风",
  permissionType.notification: "通知",
  permissionType.photo: "相册",
  permissionType.location: "定位"
};

void inspectPermission(
  BuildContext context, {
  String? content,
  List? permissionList,
  Function? permissionCallBack,
  bool isShowToast = true,
  Function? permissionErrorCallBack,
}) async {
  // SmartDialog.show(
  //   builder: (context){
  //     return Container(
  //       margin: EdgeInsets.symmetric(
  //           vertical: ScreenUtil().statusBarHeight + 20.w, horizontal: 32.w),
  //       padding: EdgeInsets.all(20.w),
  //       width: 1.sw,
  //       decoration: BoxDecoration(
  //           color: Colors.white, borderRadius: BorderRadius.circular(20.w)),
  //       child: Text(
  //         content??'',
  //         style: TextStyle(
  //             color: Colors.black, fontWeight: FontWeight.w500, fontSize: 24.sp),
  //       ),
  //     );
  //   },
  //   alignment: Alignment.topCenter,
  // );
  bool isJurisdiction = true;
  Map<Permission, PermissionStatus> map;
  List<Permission> permission = [];
  String permissionTipsStr = "";
  if (permissionList != null && permissionList.length > 0) {
    for (permissionType item in permissionList) {
      PermissionStatus status;
      permissionTipsStr += _permissionNames[item] + '、';
      switch (item) {
        case permissionType.camera:
          status = await Permission.camera.request();
          if (status != PermissionStatus.granted) {
            isJurisdiction = false;
          }
          print('获取相册权限camera === $status');
          permission.add(Permission.camera);
          break;
        case permissionType.storage:
          status = await Permission.storage.request();
          print('获取相册权限storage === $status');
          if (status != PermissionStatus.granted && Platform.isAndroid) {
            isJurisdiction = false;
          }
          permission.add(Permission.storage);
          break;
        case permissionType.photo:
          status = await Permission.photos.request();
          print('获取相册权限 === $status');

          if (status != PermissionStatus.granted &&
              status != PermissionStatus.limited) {
            isJurisdiction = false;
          }
          permission.add(Permission.photos);
          break;
        case permissionType.microphone:
          status = await Permission.microphone.request();

          if (status != PermissionStatus.granted) {
            isJurisdiction = false;
          }
          permission.add(Permission.microphone);
          break;
        case permissionType.notification:
          status = await Permission.notification.request();
          if (status != PermissionStatus.granted) {
            isJurisdiction = false;
          }
          permission.add(Permission.notification);
          break;
        case permissionType.location:
          status = await Permission.location.request();
          print('huoqu loaction');
          // if (Platform.isIOS) {
          //   status = await Permission.location.request();
          // } else {
          //   status = await Permission.location.status;
          //   if (status != PermissionStatus.denied &&
          //       status != PermissionStatus.permanentlyDenied &&
          //       status != PermissionStatus.granted) {
          //     status = await Permission.location.request();
          //   }
          // }
          if (status != PermissionStatus.granted) {
            isJurisdiction = false;
          }
          permission.add(Permission.location);
          break;
      }
    }
  }
  SmartDialog.dismiss();
  if (isJurisdiction) {
    if(permissionCallBack!= null){
      permissionCallBack!();
    }
  } else {
    if (isShowToast) {
      SmartDialog.showToast(
        "Permission error",
        alignment: Alignment.center,
      );
    }
  }
}

void getAlbumPermission(BuildContext context, Function videoCallback) {
  List typeList = [
    permissionType.photo,
  ];
  if (Platform.isAndroid) {
    typeList = [
      permissionType.storage,
    ];
  }
  String tipContent = "平台想要获取您的相册、存储权限，用于扫码识别设备号等功能";
  inspectPermission(context,
      content: tipContent,
      permissionList: typeList,
      permissionCallBack: videoCallback);
}

void getCameraPermission(BuildContext context, Function videoCallback) {
  List typeList = [
    permissionType.camera,
  ];
  if (Platform.isAndroid) {
    typeList = [
      permissionType.camera,
    ];
  }
  String tipContent = "平台想要获取您的相机、存储权限，用于扫码识别设备号等功能";
  inspectPermission(context,
      content: tipContent,
      permissionList: typeList,
      permissionCallBack: videoCallback);
}

void getVoicePermission(BuildContext context, Function videoCallback) {
  List typeList = [
    permissionType.microphone,
  ];
  String tipContent = "平台想要获取您的麦克风权限，用于录制音频用于发布教子经验分享等功能";
  inspectPermission(context,
      content: tipContent,
      permissionList: typeList,
      permissionCallBack: videoCallback);
}

void getLocationPermission(
    BuildContext context, Function videoCallback, bool isShowToast) {
  List typeList = [permissionType.location];
  String tipContent = "平台想要获取您的位置权限，以便您能够为您推荐周边精彩内容或机构，请允许。拒绝或取消授权不影响使用其他服务";
  inspectPermission(
    context,
    content: tipContent,
    permissionList: typeList,
    isShowToast: isShowToast,
    permissionCallBack: videoCallback,
  );
}
